package com.library.management.controller;

import com.library.management.common.PageRequest;
import com.library.management.common.PageResult;
import com.library.management.common.Result;
import com.library.management.entity.User;
import com.library.management.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 用户控制器
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 用户列表页面
     */
    @GetMapping("/list")
    public String list() {
        return "user/list";
    }

    /**
     * 用户详情页面
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable Long id, Model model) {
        User user = userService.getById(id);
        model.addAttribute("user", user);
        return "user/detail";
    }

    /**
     * 用户添加页面
     */
    @GetMapping("/add")
    public String add() {
        return "user/add";
    }

    /**
     * 用户编辑页面
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable Long id, Model model) {
        User user = userService.getById(id);
        model.addAttribute("user", user);
        return "user/edit";
    }

    /**
     * 个人信息页面
     */
    @GetMapping("/profile")
    public String profile() {
        return "user/profile";
    }

    /**
     * 我的借阅页面
     */
    @GetMapping("/borrow")
    public String borrow() {
        return "user/borrow";
    }

    /**
     * 查询用户列表
     */
    @GetMapping("/api/list")
    @ResponseBody
    public Result<PageResult<User>> list(User user, PageRequest pageRequest) {
        PageResult<User> pageResult = userService.list(user, pageRequest.getPageNum(), pageRequest.getPageSize());
        return Result.success(pageResult);
    }

    /**
     * 根据ID查询用户
     */
    @GetMapping("/api/get/{id}")
    @ResponseBody
    public Result<User> get(@PathVariable Long id) {
        User user = userService.getById(id);
        return Result.success(user);
    }

    /**
     * 添加用户
     */
    @PostMapping("/api/add")
    @ResponseBody
    public Result<Integer> add(@RequestBody User user) {
        int result = userService.add(user);
        return Result.success(result);
    }

    /**
     * 更新用户
     */
    @PutMapping("/api/update")
    @ResponseBody
    public Result<Integer> update(@RequestBody User user) {
        int result = userService.update(user);
        return Result.success(result);
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/api/delete/{id}")
    @ResponseBody
    public Result<Integer> delete(@PathVariable Long id) {
        int result = userService.delete(id);
        return Result.success(result);
    }

    /**
     * 登录
     */
    @PostMapping("/api/login")
    @ResponseBody
    public Result<User> login(@RequestBody User user) {
        // 根据用户名查询用户
        User dbUser = userService.getByUsername(user.getUsername());
        if (dbUser == null) {
            return Result.error("用户名不存在");
        }
        
        // 验证密码
        if (!dbUser.getPassword().equals(user.getPassword())) {
            return Result.error("密码错误");
        }
        
        // 验证状态
        if (dbUser.getStatus() == 0) {
            return Result.error("账号已被禁用");
        }
        
        return Result.success(dbUser);
    }

    /**
     * 根据用户名查询用户
     */
    @GetMapping("/api/getByUsername/{username}")
    @ResponseBody
    public Result<User> getByUsername(@PathVariable String username) {
        User user = userService.getByUsername(username);
        if (user == null) {
            return Result.error("用户不存在");
        }
        return Result.success(user);
    }
}